package com.huaxun.edu.ucc.impl;

import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import net.optix.common.app.ExecuteResult;
import net.optix.common.app.Pager;
import net.optix.common.db.IBaseDAO;

import com.huaxun.edu.model.TBE_School;
import com.huaxun.edu.ucc.ISchoolUCC;

public class SchoolUCC implements ISchoolUCC {

	private IBaseDAO baseDAO;

	private TransactionTemplate transTemplate;

	@Override
	public List<TBE_School> findAllSchools() {
		return baseDAO.query("select * from TBE_School where 1=1 ", null,
				TBE_School.class, 0, 0);
	}

	@Override
	public List<TBE_School> findSchools(Pager pager) {
		pager.recordCount = baseDAO.queryForLongWithCondition("TBE_School",
				pager.condition);
		return baseDAO.query("select * from TBE_School where 1=1 ",
				TBE_School.class, pager);
	}

	@Override
	public TBE_School findSchool(String xxdm) {
		return baseDAO.queryForObject("select * from tbe_school where xxdm=?",
				new Object[] { xxdm }, TBE_School.class);
	}

	@Override
	public ExecuteResult saveSchool(final String xxdm, final TBE_School school) {

		Object object = transTemplate.execute(new TransactionCallback() {

			ExecuteResult result = null;

			@Override
			public Object doInTransaction(TransactionStatus status) {

				try {
					long count = baseDAO.queryForLong(
							"select count(1) from tbe_school where xxdm=?",
							new Object[] { xxdm });
					if (1 == count) {
						baseDAO.updateObject(school, " and xxdm='" + xxdm + "'");
					} else {
						result = new ExecuteResult(-1, "学校已被删除或不存在");
					}
				} catch (Exception e) {
					status.setRollbackOnly();
					result = new ExecuteResult(-1, "更新学校:" + e.getMessage());
					e.printStackTrace();
				}
				return result;
			}
		});
		if (null != object && object instanceof ExecuteResult) {
			return (ExecuteResult) object;
		}
		return null;
	}

	@Override
	public ExecuteResult addSchool(final String xxdm, final TBE_School school) {
		Object object = transTemplate.execute(new TransactionCallback() {

			ExecuteResult result = null;

			@Override
			public Object doInTransaction(TransactionStatus status) {

				try {
					long count = baseDAO.queryForLong(
							"select count(1) from tbe_school where xxdm=?",
							new Object[] { xxdm });
					if (0 == count) {
						school.setXxdm(xxdm);
						baseDAO.insertObject(school);
						result = new ExecuteResult(1, "成功创建学校");
					} else {
						result = new ExecuteResult(-1, "学校代码被占用");
					}
				} catch (Exception e) {
					status.setRollbackOnly();
					result = new ExecuteResult(-1, "创建学校:" + e.getMessage());
					e.printStackTrace();
				}
				return result;
			}
		});
		if (null != object && object instanceof ExecuteResult) {
			return (ExecuteResult) object;
		}
		return null;
	}

	@Override
	public ExecuteResult deleteSchool(String xxdm) {
		
		return null;
	}

	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public TransactionTemplate getTransTemplate() {
		return transTemplate;
	}

	public void setTransTemplate(TransactionTemplate transTemplate) {
		this.transTemplate = transTemplate;
	}

}